package tech.aistar.corejava.day15;

/**
 * @Author: lya
 * @Description: 本类的作用描述:死锁
 * @CreateDate: 2019/8/4
 * @Version: 1.0
 */
public class DeadLockDemo {
    //抢占资源

    //静态域容易产生死锁
    static Object oo1 = new Object();
    static Object oo2 = new Object();

    public static void main(String[] args) {
        Thread t1 = new T1();
        Thread t2 = new T2();

        t1.start();
        t2.start();
    }

}
class T1 extends Thread{
    private DeadLockDemo dd = new DeadLockDemo();

    @Override
    public void run(){
        synchronized (dd.oo1){//拥有临界资源dd.oo1的"锁标记" - 执行同步代码块
            System.out.println("===========11==========");

            try {
                //让另一个线程拥有足够时间去抢到dd.oo2
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            synchronized (dd.oo2){
                System.out.println("===========12===========");
            }
        }
    }
}
class T2 extends Thread{
    private DeadLockDemo dd = new DeadLockDemo();

    @Override
    public void run(){
        synchronized (dd.oo2){
            System.out.println("========21=========");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (dd.oo1){
                System.out.println("========21========");
            }
        }
    }
}
